package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.entity.RainfallInfiltrationRate;
import com.sevenyear.excelanalyze.entity.param.entityParam.RainfallInfiltrationRateParam;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import com.sevenyear.excelanalyze.service.RainfallInfiltrationRateService;
import org.springframework.beans.BeanUtils;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/22 14:02
 */
public class RainfallInfiltrationRateListener extends AnalysisEventListener<RainfallInfiltrationRate> {

    private RainfallInfiltrationRateService rainfallInfiltrationRateService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public RainfallInfiltrationRateListener() {
    }

    public RainfallInfiltrationRateListener(RainfallInfiltrationRateService rainfallInfiltrationRateService, PartitionBaseInfoService partitionBaseInfoService) {
        this.rainfallInfiltrationRateService = rainfallInfiltrationRateService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(RainfallInfiltrationRate rainfallInfiltrationRate, AnalysisContext analysisContext) {
        //计算平均值
        float average = CalculateUtils.average(rainfallInfiltrationRate.getRainfallInfiltrationRateMin(), rainfallInfiltrationRate.getRainfallInfiltrationRateMax());
        rainfallInfiltrationRate.setRainfallInfiltrationRateAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",rainfallInfiltrationRate.getLandform()).eq("clay_thickness",rainfallInfiltrationRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        rainfallInfiltrationRate.setPartitionId(partitionBaseInfo.getId());
        rainfallInfiltrationRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        rainfallInfiltrationRate.setAreaCode(rainfallInfiltrationRate.getArea()+rainfallInfiltrationRate.getPartitionCode());
        UpdateWrapper<RainfallInfiltrationRate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code",rainfallInfiltrationRate.getAreaCode());
        rainfallInfiltrationRateService.saveOrUpdate(rainfallInfiltrationRate,updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
